//
//  JKPodManager.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/5.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit
import Kingfisher

class JKPodManager: NSObject {
    
}

// MARK: - Kingfisher

extension UIImageView {
    
    /** 设置图片 */
    public func jk_setCircleIcon(withUrlStr: String?) {
        
        guard let _ = withUrlStr else { return }
        
        kf.setImage(with: URL(string: withUrlStr!), placeholder: JKAccountManager.shared.headerIconPlaceHolder, options: nil, progressBlock: nil) { (image, error, cacheType, imageURL) in
            
            
            if image == nil {
                return
            }
            
            DispatchQueue.global().async {
                
                let img = image?.jk_circleImage()
                
                DispatchQueue.main.async {
                    
                    self.image = img
                }
            }
        }
    }
    
    /** 设置图片 */
    public func jk_setImage(withUrlStr: String?) {
        
        jk_setImage(withUrlStr: withUrlStr, placeHolder: nil)
    }
    
    /** 设置图片 带placeHolder */
    public func jk_setImage(withUrlStr: String?, placeHolder: String?) {
        
        jk_setImage(withUrlStr: withUrlStr, placeHolder: placeHolder, progressBlock: nil, completionHandler: nil)
    }
    
    /** 设置图片 带placeHolder */
    public func jk_setImage(withUrlStr: String?, placeHolder: String?, progressBlock: ((_ receivedSize: Int64, _ totalSize: Int64) -> Void)?, completionHandler:((_ image: Image?, _ error: NSError?, _ imageURL: URL?) -> Void)?) {
        
        guard let _ = withUrlStr else { return }
        
        var image: UIImage?
        
        if let holder = placeHolder {
            
            image = UIImage(named: holder)
        }
        
        kf.setImage(with: URL(string: withUrlStr!), placeholder: image, options: [.transition(ImageTransition.fade(0.5))], progressBlock: progressBlock) { (image, error, cacheType, imageURL) in
            
            if completionHandler == nil { return }
            
            completionHandler!(image, error, imageURL)
        }
    }
    
    //    public func setImage(with resource: Resource?,
    //                         placeholder: Placeholder? = nil,
    //                         options: KingfisherOptionsInfo? = nil,
    //                         progressBlock: DownloadProgressBlock? = nil,
    //                         completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
}

extension UIImage {
    
    /** 切圆角 */
    public func jk_circleImage() -> UIImage {
        
        return jk_circleImage(borderWidth: nil, borderColor: nil)
    }
    
    /** 切圆角 */
    public func jk_circleImage(borderWidth: CGFloat?, borderColor: UIColor?) -> UIImage {
        
        var width = self.size.width
        var height = self.size.height
        
        if (width != height) {
            
            width = min(width, height)
        }
        
        width = width > 100 ? 100 : width
        height = width
        
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), false, 0.0)
        
        let context = UIGraphicsGetCurrentContext()
        
        let rect = CGRectMake(0, 0, width, height)
        
        context?.addEllipse(in: rect)
        
        // 裁剪
        context?.clip()
        
        // 将图片画上去
        draw(in: rect)
        
        if borderWidth != nil && borderColor != nil {
            
            borderColor!.set()
            
            context?.setLineWidth(borderWidth!)
            context?.addEllipse(in: rect)
            context?.strokePath()
        }
        
        let image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        return image ?? self
    }
    
}
